雖然前面有提到雖然 K8S 內建有 load balance 機制,但是建立在 L4 上。所以只有不具備真正持久連線的 HTTP 可以使用。但真正具有持久連線的 gRPC,L4 就無法進行幫忙,導致擴展 pod 之後無法將流量導過去新的 pod 上,所以我們引用 app mesh 功能。
但 AWS 有提出不需要這麼麻煩建立 app mesh 的 解法,就是直接使用 AWS ALB ,在 2020/10 ,ALB 支援了 HTTP/2 and gRPC load balancing,這邊我們就來了解內容。
git clone https://github.com/grpc/grpc/tree/master/examples/python/route_guide
建立 Docker file
FROM python:3.7
RUN pip install protobuf grpcio
COPY ./grpc/examples/python/route_guide .
CMD python route_guide_server.py
EXPOSE 50051
上傳到 ECR
aws ecr get-login-password --region eu-north-1 | docker login --username AWS --password-stdin 123412341234.dkr.ecr.eu-north-1.amazonaws.com
docker build -t route-guide .
docker tag route-guide:latest 123412341234.dkr.ecr.eu-north-1.amazonaws.com/route-guide:latest
docker push 123412341234.dkr.ecr.eu-north-1.amazonaws.com/route-guide:latest
建立 EKS cluster
建立 security group,並允許自己的電腦IP位置可以 TCP 連線到此 SG
建立 TG
apiVersion: v1
kind: Service
metadata:
name: route-guide
spec:
ports:
- port: 50051
targetPort: 50051
protocol: TCP
name: grpc
selector:
app: route-guide
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: route-guide-ingress
annotations:
kubernetes.io/ingress.class: alb
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}]'
alb.ingress.kubernetes.io/backend-protocol-version: HTTP
alb.ingress.kubernetes.io/group.name: route-guide
spec:
rules:
- http:
paths:
- path: /*
pathType: ImplementationSpecific
backend:
service:
name: route-guide
port:
number: 50051
sevice file
apiVersion: v1
kind: Service
metadata:
name: eks-service
spec:
selector:
app: your-app-name
ports:
- protocol: TCP
port: 50051
targetPort: 50051
type: LoadBalancer
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: route-guide-ingress
annotations:
kubernetes.io/ingress.class: alb
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
spec:
rules:
- http:
paths:
- path: /*
pathType: ImplementationSpecific
backend:
service:
name: route-guide
port:
number: 50051